package com.vtuzx.kingdee.xqc_api.web;

import com.vtuzx.core.VtuzxException;
import com.vtuzx.core.bean.dto.VtuzxMap;
import com.vtuzx.core.bean.dto.VtuzxToken;
import com.vtuzx.core.constant.IVtuzxConst;
import com.vtuzx.core.utils.VtuzxUtil;
import com.vtuzx.kingdee.xqc_api.service.IXqcPurchaseService;
import com.vtuzx.web.constant.ApiType;
import com.vtuzx.web.validator.VtuzxValidator;
import com.vtuzx.web.web.VtuzxApi;
import com.vtuzx.web.web.VtuzxControlParam;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 采购入库
 */
@RequestMapping("/${vtuzx.fast.api.path}/${vtuzx.fast.api.version}/pur_in")
@RestController
public class XqcPurInController {

    /** 采购服务 */
    @Autowired
    private IXqcPurchaseService purchaseService;

    /**
     * 保存采购入库单(到审核阶段)
     */
    @PostMapping("/save_user_purin_by_purcol_list")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "billNoList", label = "采购收货单号列表", notNull = true)
    } )
    public Object saveUserPurinByPurcolList(@VtuzxControlParam VtuzxToken token, @SuppressWarnings("unused") @RequestBody Map<String, Object> param) throws Exception {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        List<String> billNoList = VtuzxUtil.getObject(param, "billNoList");
        String stockNo = VtuzxUtil.getString(param, "stockNo");
        String locationNo = VtuzxUtil.getString(param, "locationNo");
        String source = VtuzxUtil.getString(param, "source");
        boolean isBc = VtuzxUtil.getBoolean(param, "isBc");
        purchaseService.saveUserPurInByPurColList(_userId, name, stockNo, locationNo, billNoList, source, isBc);
        return null;
    }

    /**
     * 保存采购入库单(到创建阶段)
     */
    @PostMapping("/save_user_purin")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "stockNo", label = "仓库", notNull = true)
    } )
    public Object saveUserPurIn(@VtuzxControlParam VtuzxToken token, @SuppressWarnings("unused") @RequestBody Map<String, Object> param) throws Exception {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        String stockNo = VtuzxUtil.getString(param, "stockNo");
        String source = VtuzxUtil.getString(param, "source");
        boolean isBc = VtuzxUtil.getBoolean(param, "isBc");
        String orderNo = VtuzxUtil.getString(param, "orderNo");
        purchaseService.saveUserPurIn(_userId, name, stockNo, source, isBc, orderNo);
        return null;
    }


    /**
     * 保存采购入库单(到审核阶段)
     */
    @PostMapping("/audit_purin_order")
    @VtuzxApi
    public Object auditPurInOrder(@VtuzxControlParam VtuzxToken token, @SuppressWarnings("unused") @RequestBody Map<String, Object> param) throws Exception {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        String stockNo = VtuzxUtil.getString(param, "stockNo");
        String source = VtuzxUtil.getString(param, "source");
        boolean isBc = VtuzxUtil.getBoolean(param, "isBc");
        String billNo = VtuzxUtil.getString(param, "billNo");
        purchaseService.auditPurInOrder(_userId, name, stockNo, billNo, source, isBc);
        return null;
    }

    /**
     * 添加用户临时采购入库物料箱号
     */
    @PostMapping("/add_user_purin_material_box")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "stockNo", label = "仓库", notNull = true),
            @VtuzxValidator(key = "locationNo", label = "仓位", notNull = true),
            @VtuzxValidator(key = "boxNo", label = "箱号", notNull = true),
            @VtuzxValidator(key = "count", label = "数量", notNull = true, isNumber = true)
    } )
    public Object addUserPurInMaterialBox(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        String stockNo = VtuzxUtil.getString(param, "stockNo");
        String locationNo = VtuzxUtil.getString(param, "locationNo");
        String boxNo = VtuzxUtil.getString(param, "boxNo");
        double count = VtuzxUtil.getDouble(param, "count");
        return purchaseService.addUserPurInMaterialBox(_userId, name, stockNo, locationNo, boxNo, count);
    }

    /**
     * 添加用户临时采购入库物料箱号
     */
    @PostMapping("/add_user_purin_material_box_list")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "stockNo", label = "仓库", notNull = true),
            @VtuzxValidator(key = "locationNo", label = "仓位", notNull = true),
            @VtuzxValidator(key = "boxList", label = "箱号列表", notNull = true)
    } )
    public Object addUserPurInMaterialBoxList(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        String stockNo = VtuzxUtil.getString(param, "stockNo");
        String locationNo = VtuzxUtil.getString(param, "locationNo");
        List<Map<String, Object>> boxList = VtuzxUtil.getObject(param, "boxList");
        boolean isBc = VtuzxUtil.getBoolean(param, "isBc");
        String orderNo = VtuzxUtil.getString(param, "orderNo");
        String billNo = VtuzxUtil.getString(param, "billNo");
        return purchaseService.addUserPurInMaterialBoxList(_userId, name, stockNo, locationNo, boxList, isBc, orderNo, billNo);
    }

    /**
     * 删除用户临时采购入库物料箱号
     */
    @PostMapping("/delete_user_purin_material_box")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "id", label = "id", notNull = true)
    } )
    public Object deleteUserPurInMaterialBox(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String id = VtuzxUtil.getString(param, "id");
        return purchaseService.deleteUserPurInMaterialBox(_userId, id);
    }

    /**
     * 删除用户临时采购入库物料
     */
    @PostMapping("/delete_user_purin_material")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "id", label = "id", notNull = true)
    } )
    public Object deleteUserPurInMaterial(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String id = VtuzxUtil.getString(param, "id");
        purchaseService.deleteUserPurInMaterial(_userId, id);
        return null;
    }

    /**
     * 删除用户临时采购入库单(清空)
     */
    @PostMapping("/delete_user_purin")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "stockNo", label = "仓库", notNull = true)
    } )
    public Object deleteUserPurIn(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String stockNo = VtuzxUtil.getString(param, "stockNo");
        purchaseService.deleteUserPurIn(_userId, stockNo);
        return null;
    }

    /**
     * 查询已创建包材采购入库单列表
     */
    @PostMapping("/get_created_bcPurin_list")
    @VtuzxApi
    public Object getCreatedBcPurInList(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String orderNo = VtuzxUtil.getString(param,"orderNo");
        return purchaseService.getCreatedBcPurInList(_userId, orderNo);
    }

    /**
     * 编辑入库物料箱号重量
     */
    @PostMapping("/update_user_purin_material_box")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "boxNo", label = "箱号", notNull = true),
            @VtuzxValidator(key = "count", label = "重量", notNull = true, isNumber = true)
    } )
    public Object updateUserPurInMaterialBox(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String boxNo = VtuzxUtil.getString(param, "boxNo");
        double count = VtuzxUtil.getDouble(param, "count");
        return purchaseService.updateUserPurInMaterialBox(_userId, boxNo, count);
    }

    /**
     * 获取用户的入库物料列表
     */
    @PostMapping("/get_user_purin_material")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "stockNo", label = "仓库", notNull = true),
    } )
    public Object getUserPurInMaterial(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String stockNo = VtuzxUtil.getString(param, "stockNo");
        String orderNo = VtuzxUtil.getString(param, "orderNo");
        String billNo = VtuzxUtil.getString(param, "billNo");
        boolean isBc = VtuzxUtil.getBoolean(param, "isBc");
        List<String> excList = new ArrayList<>();
        return purchaseService.getUserPurInMaterial(_userId, stockNo, isBc, orderNo, excList, billNo);
    }

    /**
     * 获取用户的入库物料箱号列表
     */
    @PostMapping("/get_user_purin_material_box")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "stockNo", label = "仓库", notNull = true),
            @VtuzxValidator(key = "id", label = "入库物料单据id", notNull = true)
    } )
    public Object getUserPurInMaterialBox(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String id = VtuzxUtil.getString(param, "id");
        String billNo = VtuzxUtil.getString(param, "billNo");
        return purchaseService.getUserPurInMaterialBox(_userId, id, billNo);
    }

    /**
     * 获取用户的采购入库单
     */
    @PostMapping("/get_purin_bill")
    @VtuzxApi
    public Object getPurInBill(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        int pageIndex = VtuzxUtil.getInt(param, IVtuzxConst.Key_pageIndex);
        int pageSize = VtuzxUtil.getInt(param, IVtuzxConst.Key_pageSize, 10);
        return purchaseService.getPurInBill(_userId, pageIndex, pageSize, param);
    }

    /**
     * 获取用户的采购入库单BN
     */
    @PostMapping("/get_purin_bill_bn")
    @VtuzxApi
    public Object getPurInBillBn(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        int pageIndex = VtuzxUtil.getInt(param, IVtuzxConst.Key_pageIndex);
        int pageSize = VtuzxUtil.getInt(param, IVtuzxConst.Key_pageSize, 10);
        return purchaseService.getPurInBillBn(_userId, pageIndex, pageSize, param);
    }

    /**
     * 获取用户的采购入库单分录列表
     */
    @PostMapping("/get_purin_bill_detail")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "stockNo", label = "仓库", notNull = true),
            @VtuzxValidator(key = "stockInBillNo", label = "入库单号", notNull = true)
    } )
    public Object getPurInBillDetail(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String stockNo = VtuzxUtil.getString(param, "stockNo");
        String stockInBillNo = VtuzxUtil.getString(param, "stockInBillNo");
        return purchaseService.getPurInBillDetail(_userId, stockNo, stockInBillNo);
    }

    /**
     * 获取用户的采购入库单分录列表BN
     */
    @PostMapping("/get_purin_bill_detail_bn")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "stockNo", label = "仓库", notNull = true),
            @VtuzxValidator(key = "stockInBillNo", label = "入库单号", notNull = true)
    } )
    public Object getPurInBillDetailBn(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String stockNo = VtuzxUtil.getString(param, "stockNo");
        String stockInBillNo = VtuzxUtil.getString(param, "stockInBillNo");
        return purchaseService.getPurInBillDetailBn(_userId, stockNo, stockInBillNo);
    }


    /**
     * 获取用户的采购入库单分录的箱号详细
     */
    @PostMapping("/get_purin_bill_detail_box")
    @VtuzxApi
    public Object getPurInBillDetailBox(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String _detailId = VtuzxUtil.getString(param, "_detailId");
        return purchaseService.getPurInBillDetailBox(_userId, _detailId);
    }

    /**
     * 根据采购入库单号获取详细信息
     */
    @PostMapping("/get_purin_bill_by_bill_no")
    @VtuzxApi
    public Object getPurInBillByBillNo(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String billNo = VtuzxUtil.getString(param, "billNo");
        return purchaseService.getPurInBillByBillNo(_userId, billNo);
    }

    /**
     * 生成采购退货单
     */
    @PostMapping("/add_pur_return")
    @VtuzxApi
    public Object addPurReturn(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws Exception {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token,"name");
        purchaseService.addPurReturn(name, _userId, param);
        return null;
    }

    /**
     * 获取用户的采购入库单
     */
    @PostMapping("/get_pur_return_bill")
    @VtuzxApi
    public Object getPurReturnBill(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        int pageIndex = VtuzxUtil.getInt(param, IVtuzxConst.Key_pageIndex);
        int pageSize = VtuzxUtil.getInt(param, IVtuzxConst.Key_pageSize, 10);
        return purchaseService.getPurReturnBill(_userId, pageIndex, pageSize, param);
    }

    /**
     * 获取采购退料详情信息
     */
    @PostMapping("/get_pur_return_by_billNo")
    @VtuzxApi
    public Object getPurReturnByBillNo(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String billNo = VtuzxUtil.getString(param,"billNo");
        return purchaseService.getPurReturnByBillNo(_userId, billNo);
    }

    /**
     * 导出采购入库单
     */
    @PostMapping("/download_pur_in_list")
    @VtuzxApi(type = ApiType.Download)
    public Object downloadPurInList(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws Exception {
        return purchaseService.downloadPurInList(token, param);
    }
}
